import axios from 'axios';
import qs from 'qs'
import { Message } from 'element-ui';

axios.defaults.timeout = 5000; //超时终止请求
axios.defaults.baseURL = 'http://localhost:8888/'; //配置请求地址

//http request 拦截器
axios.interceptors.request.use(
	config => {
		config.data = qs.stringify(config.data);
		config.headers = {
			'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
		}
		const token = localStorage.getItem("appUserToken");
		if(token != '' && token != null && token != 'undefined'){
			config.params = {'userToken': token}
		}else{
			config.params = {'userToken': ''}
		}
		return config;
	},
	error => {
		return Promise.reject(err);
	}
);

//http response 拦截器
axios.interceptors.response.use(
	response => {
		if(response.data.errCode == 2) {
			router.push({
				path: "/login",
				querry: {
					redirect: router.currentRoute.fullPath
				} //从哪个页面跳转
			})
		}
		return response;
	},
	error => {
		Message({
			Message: error.message,
			type: 'error',
			duration: 3 * 1000
		});
		return Promise.reject(error)
	}
)

/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */
export function fetch(url, params = {}) {
	return new Promise((resolve, reject) => {
		axios.get(url, {
			params: params
		}).then(response => {
			if(response.data.returnCode === '401'){
				Message({
					message: '登录超时，请重新登录',
					type: 'error',
					duration: 3 * 1000
				});
				localStorage.removeItem('appUserToken');
				const token = localStorage.getItem("appUserToken");
   				localStorage.removeItem(token);
   				this.$router.push({path: '/login'});
			}else{
				resolve(response.data);
			}
		}).catch(err => {
			reject(err)
		})
	})
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function post(url, data = {}) {
	return new Promise((resolve, reject) => {
		axios.post(url, data).then(response => {
			if(response.data.returnCode === '401'){
				Message({
					message: '登录超时，请重新登录',
					type: 'error',
					duration: 3 * 1000
				});
				localStorage.removeItem('appUserToken');
				const token = localStorage.getItem("appUserToken");
   				localStorage.removeItem(token);
   				this.$router.push({path: '/login'});
			}else{
				resolve(response.data);
			}
		}, err => {
			reject(err)
		})
	})
}

/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function patch(url, data = {}) {
	return new Promise((resolve, reject) => {
		axios.patch(url, data).then(response => {
			if(response.data.returnCode === '401'){
				Message({
					message: '登录超时，请重新登录',
					type: 'error',
					duration: 3 * 1000
				});
				localStorage.removeItem('appUserToken');
				const token = localStorage.getItem("appUserToken");
   				localStorage.removeItem(token);
   				this.$router.push({path: '/login'});
			}else{
				resolve(response.data);
			}
		}, err => {
			reject(err)
		})
	})
}

/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function put(url, data = {}) {
	return new Promise((resolve, reject) => {
		axios.put(url, data).then(response => {
			if(response.data.returnCode === '401'){
				Message({
					message: '登录超时，请重新登录',
					type: 'error',
					duration: 3 * 1000
				});
				localStorage.removeItem('appUserToken');
				const token = localStorage.getItem("appUserToken");
   				localStorage.removeItem(token);
   				this.$router.push({path: '/login'});
			}else{
				resolve(response.data);
			}
		}, err => {
			reject(err)
		})
	})
}